package com.dy.controller.show;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dy.pojo.entity.DyBlogEntity;
import com.dy.pojo.entity.DyTagEntity;
import com.dy.pojo.entity.DyTypeEntity;
import com.dy.pojo.vo.DyShowTagVO;
import com.dy.pojo.vo.DyShowTypeVO;
import com.dy.service.DyBlogService;
import com.dy.service.DyTagService;
import com.dy.service.DyTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName:
 * @Author: 仄言in
 * @Desctiption: (描述)用来进行首页的分页展示
 * @Date: 2021/8/5 17:01
 * @Version 1.0 (版本)
 */
@Controller
public class DyShowBlogController {
    @Autowired
    private DyBlogService dyBlogService;
    @Autowired
    private DyTypeService dyTypeService;
    @Autowired
    private DyTagService dyTagService;
    /**
     * (博客首页的展示)
     * 博客分页查询
     * 取分类最多的前六个
     * 标签全查询分页
     * 最新日期的前四条博客
     * @param page
     * @param pageSize
     * @param model
     * @return
     **/
    @RequestMapping("/")
    public String blog_index(@RequestParam(value="page",defaultValue ="1")Integer page,
                             @RequestParam(value = "pageSize",defaultValue = "6")Integer pageSize, Model model){
        Page<DyBlogEntity> selectlist = dyBlogService.selectlist(page, pageSize);
        model.addAttribute("page",selectlist);
        model.addAttribute("types",dyBlogService.queryBy6());
        model.addAttribute("tags",dyTagService.listShowTag());
        model.addAttribute("newstblog",dyBlogService.selectNewstBlog());
        return "/index";
    }
    /**
     * 全局模糊搜索
     * @param page
     * @param pageSize
     * @param model
     * @param query
     * @return
     **/
    @GetMapping("/search")
    public String blog_search(@RequestParam(value="page",defaultValue ="1")Integer page,
                             @RequestParam(value = "pageSize",defaultValue = "6")Integer pageSize, Model model,@RequestParam("query") String query){
        model.addAttribute("page",dyBlogService.queryBySearch(page,pageSize,query));
        model.addAttribute("query",query);
        return "/search";
    }
    /**
     * 博客单查询详情页
     * @param id
     * @param model
     * @return
     **/
    @RequestMapping("/blog/{id}")
    public String blog_blog(@PathVariable Long id,Model model){
        model.addAttribute("blog",dyBlogService.byBlogs(id));
        model.addAttribute("tags",dyTagService.selectShowTags(id));
        return "/blog";
    }
    /**
     * 博客分类页面展示
     * @param model
     * @return
     **/
    @RequestMapping("/types")
    public String types_blog(@RequestParam(value="page",defaultValue ="1")Integer page,
                             @RequestParam(value = "pageSize",defaultValue = "4")Integer pageSize,Model model){
        model.addAttribute("blogs",dyBlogService.queryBy6Type(page,pageSize));
        model.addAttribute("type",dyBlogService.queryBy6());
        return "/types";
    }
    @RequestMapping("/typeId/{name}")
    public String typeId_blog(Model model,@PathVariable String name){
        model.addAttribute("blogs",dyBlogService.selectBlogByName(name));
        return null;
    }

    /**
     * 标签全查询分页
     * @param Model
     * @return
     **/
    @RequestMapping("/tag")
    @ResponseBody
    public List<DyShowTagVO> tag(Model Model){

        List<DyShowTagVO> tagVOList = dyTagService.listShowTag();
        Model.addAttribute("page",tagVOList);
        return tagVOList;
    }

}
